package com.wanhai.itm.wditm146.vm;

import com.wanhai.zkbase.core.WHL_BaseVM;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.richfaces.event.DragEvent;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.event.DropEvent;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;

/**
 * a prototype of editm146
 *
 * @author Richard Shih
 */
public class WDITM146_VM extends WHL_BaseVM {

    private List<Map<String, Object>> groups = new ArrayList<Map<String, Object>>();
    private List<Map<String, Object>> members = new ArrayList<Map<String, Object>>();

    @Init(superclass = true)
    public void Init() {
        //group data
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "AP1");
        map.put("GROUP_DESC", "系統設計一課");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "AP2");
        map.put("GROUP_DESC", "系統設計二課");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "QAD");
        map.put("GROUP_DESC", "系統品管課");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "IAD");
        map.put("GROUP_DESC", "資訊應用課");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "SSD");
        map.put("GROUP_DESC", "系統服務課");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "WEBTEAM");
        map.put("GROUP_DESC", "Web Team");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "REPORTTEAM");
        map.put("GROUP_DESC", "Report Team");
        groups.add(map);

        map = new HashMap<String, Object>();
        map.put("GROUP_NAME", "SWINGTEAM");
        map.put("GROUP_DESC", "Swing Team");
        groups.add(map);

        //group members
        map = new HashMap<String, Object>();
        map.put("MEMBER_USER_CODE", "M1933");
        map.put("MEMBER_NAME", "Richard Shih");
        members.add(map);
        
        map = new HashMap<String, Object>();
        map.put("MEMBER_USER_CODE", "M1175");
        map.put("MEMBER_NAME", "Grace Liu");
        members.add(map);        
    }
//<editor-fold defaultstate="collapse" desc="commands">
    @Command("onDrop")
    public void onDrop(@ContextParam(ContextType.TRIGGER_EVENT)DropEvent event){
         String memberUserCode,memberName;
        if(event.getDragged() instanceof Treeitem){
            Treeitem item = (Treeitem)event.getDragged();
            Treecell cell = (Treecell)item.getTreerow().getChildren().get(0);
            memberUserCode = cell.getLabel();
            cell = (Treecell)item.getTreerow().getChildren().get(1);
            memberName = cell.getLabel();
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("MEMBER_USER_CODE", memberUserCode);
            map.put("MEMBER_NAME", memberName);
            members.add(map);
            item.detach();
            BindUtils.postNotifyChange(null,null, this, "members");
        }else if(event.getDragged() instanceof Listitem){
            Listitem sItem = (Listitem)event.getDragged();
            Listcell cell = (Listcell) sItem.getChildren().get(0);
            memberUserCode = cell.getLabel();
            cell = (Listcell) sItem.getChildren().get(1);
            memberName = cell.getLabel();
            
            Treeitem newItem = new Treeitem();
            Treerow newRow = new Treerow();
            Treecell tCell = new Treecell(memberUserCode);
            newRow.appendChild(tCell);
            tCell = new Treecell(memberName);
            newRow.appendChild(tCell);
            newItem.appendChild(newRow);
            
            ((Treeitem)event.getTarget()).getTreechildren().appendChild(newItem);

            sItem.detach();
        }
    }
//</editor-fold>
//<editor-fold defaultstate="collapse" desc="getter & setter">
        public List<Map<String, Object>> getGroups() {
        return groups;
    }

    public void setGroups(List<Map<String, Object>> groups) {
        this.groups = groups;
    }

    public List<Map<String, Object>> getMembers() {
        return members;
    }

    public void setMembers(List<Map<String, Object>> members) {
        this.members = members;
    }
//</editor-fold>

    
}
